﻿/*
 * User: RSny
 * Date: 5/10/2012
 * Time: 10:05 AM
 */
using System.Runtime.InteropServices;

namespace Aspects.Custom.Audio
{
	[StructLayout(LayoutKind.Sequential)] 
	public class WaveFormat
	{
		public short wFormatTag;
		public short nChannels;
		public int nSamplesPerSec;
		public int nAvgBytesPerSec;
		public short nBlockAlign;
		public short wBitsPerSample;
		public short cbSize;

		public WaveFormat(int rate, int bits, short channels)
		{
			wFormatTag = (short)WaveFormats.Pcm;
			nChannels = channels;
			nSamplesPerSec = rate;
			wBitsPerSample = (short)bits;
			cbSize = 0;
               
			nBlockAlign = (short)(nChannels * (wBitsPerSample / 8));
			nAvgBytesPerSec = nSamplesPerSec * nBlockAlign;
		}
		
        public int ConvertLatencyToByteSize(int milliseconds)
        {
            int bytes = (int) ((nAvgBytesPerSec/1000.0)*milliseconds);
            if ((bytes%nBlockAlign) != 0)
            {
                // Return the upper BlockAligned
                bytes = bytes + nBlockAlign - (bytes % nBlockAlign);
            }
            return bytes;
        }
	}

}